
package com.epam.android.training.day3;

import com.epam.android.training.day3.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private static final String TAG = "initialActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        final Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
                startActivityForResult(intent, 0);

            }
        });
        // for getLastNonConfigurationInstance demonstration
        loadData();
        Log.i(TAG, "onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        // when second activity is coming to the top, this activity is stopped
        super.onStop();
        Log.i(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(this.toString(), "onDestroy");

    }

    @Override
    protected void onRestart() {
        // when user returns to this activity, onRestart calls, after that
        // onStart will be called
        // Another activity will be stopped and destroyed
        super.onRestart();
        Log.i(TAG, "onRestart");
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        final String[] list = new String[] {
                "wer", "rrr"
        };
        Log.i(TAG, "onRetainNonConfigurationInstance");
        return list;

    }

    private void loadData() {
        final Object data = getLastNonConfigurationInstance();
        // The activity is starting for the first time, load the data...
        if (data == null) {
            Log.i(TAG, "Loading new data");
        } else {
            // The activity was destroyed/created automatically, trying to get
            // data from prevoius activity
            Log.i(TAG, ((String[]) data)[0]);
        }

    }
}
